package com.xiaoyu.merchant.ui.activity.wallet;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.adapter.AllPowerfulAdapter;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.PreferencesUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.model.WalletDetalsListBean;
import com.xiaoyu.merchant.network.NetworkManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ParticularsActivity extends BaseActivityWithTitle {

    public static final String BILL_WALLET_ID = "bill_wallet_id";
    @BindView(R.id.common_recycler_view)
    RecyclerView mRecyclerView;
    private AllPowerfulAdapter mAdapter;
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.common_recycler_view_empty_page)
    ImageView mEmptyImg;
    private List<WalletDetalsListBean.DataBean.ListBean> mList = new ArrayList<>();
    private int page = 1;
    private int totalPage;

    @Override
    protected void loadContentView(ViewGroup parent) {
        setTitleText("明细");
        View mView = LayoutInflater.from(this).inflate(R.layout.common_recycler_view, parent, true);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setPadding(0, 16, 0, 0);
        mRecyclerView.setBackgroundColor(getResources().getColor(R.color.color_gray_F3));

        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsHeader(context).setDrawableSize(20);
            }
        });
        mRefreshLayout.setOnRefreshListener(onRefreshListener);
        mRefreshLayout.setOnLoadMoreListener(onLoadMoreListener);

    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        mList.clear();
        getParticulsData();
    }

    //解析数据
    private void getParticulsData() {
        NetworkManager.walletList(PreferencesUtil.getString(PreferencesUtil.TOKEN, ""), page, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                succeedResult(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    private void succeedResult(String result) {
        WalletDetalsListBean.DataBean bean = new Gson().fromJson(result, WalletDetalsListBean.DataBean.class);
        totalPage = Integer.parseInt(bean.getCount());
        if (bean.getList()==null) {
            mRefreshLayout.setVisibility(View.GONE);
            mEmptyImg.setVisibility(View.VISIBLE);
            mEmptyImg.setImageResource(R.mipmap.no_data);
        } else {
            mList.addAll(bean.getList());
            setAdapter(mList);
        }
        mAdapter.notifyDataSetChanged();
        if (totalPage <= 1) {
            mRefreshLayout.setEnableLoadMore(false);
        } else {
            mRefreshLayout.setEnableLoadMore(true);
        }
    }


    //设置适配器显示数据
    private void setAdapter(List<WalletDetalsListBean.DataBean.ListBean> mList) {
        mAdapter = new AllPowerfulAdapter<WalletDetalsListBean.DataBean.ListBean>(R.layout.particulars_item, mList, new AllPowerfulAdapter.OnClickListener<WalletDetalsListBean.DataBean.ListBean>() {
            @Override
            public void setOnClick(BaseViewHolder baseViewHolder, WalletDetalsListBean.DataBean.ListBean particularsBean, int position) {
                Intent intent = new Intent(ParticularsActivity.this, OrderDetailsActivity.class);
                intent.putExtra(BILL_WALLET_ID, particularsBean.getBalanceid());//账单ID
                startActivity(intent);
            }
        }) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, WalletDetalsListBean.DataBean.ListBean listBean) {
                super.convert(baseViewHolder, listBean);

                baseViewHolder.setText(R.id.particulars_order_name, listBean.getTrade_description());
                baseViewHolder.setText(R.id.particulars_order_time, listBean.getTrade_time());
                baseViewHolder.setText(R.id.particulars_order_money, listBean.getOccurrence_amount());
            }
        };

        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * 数据刷新
     */
    private OnRefreshListener onRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            page = 1;
            mList.clear();
            mAdapter.notifyDataSetChanged();
            getParticulsData();
            mRefreshLayout.finishRefresh();
            mRefreshLayout.setNoMoreData(false);
        }
    };


    /**
     * 加载更多
     */
    private OnLoadMoreListener onLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            if (page < totalPage) {
                page = page + 1;
                getParticulsData();
                mRefreshLayout.finishLoadMore();
                mRefreshLayout.setNoMoreData(true);
            } else {
                ToastUtil.showToast(ParticularsActivity.this, "已经没有更多数据！");
                mRefreshLayout.finishLoadMore();
                mRefreshLayout.setNoMoreData(false);
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
    };

}
